﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using kim.Common;

namespace kim
{
    public partial class AddorEditProductList : kim.UI.ManagePage
    {
        protected int typeid;
        private int Nid;
        protected string type;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["type"] = KIMRequest.GetString("type");
                ViewState["Nid"] = KIMRequest.GetInt("id", 0);
                ViewState["typeid"] = KIMRequest.GetInt("typeid", 0);
                Nid = KIMRequest.GetInt("id", 0);
                labtitle.Text = "内容添加";
                TreeBind(int.Parse(ViewState["typeid"].ToString()));
                GetRequestValues();
            }
        }

        #region 绑定类别=================================
        private void TreeBind(int _channel_id)
        {
            BLL.CClass bll = new BLL.CClass();
            DataTable dt = bll.GetList(0, _channel_id);

            this.ddlParentId.Items.Clear();
            this.ddlParentId.Items.Add(new ListItem("无父级分类", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["Depth"].ToString());
                string Title = dr["ClassName"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlParentId.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlParentId.Items.Add(new ListItem(Title, Id));
                }
            }
        }
       

        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ViewState["type"].ToString() == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(int.Parse(ViewState["Nid"].ToString())))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (ViewState["type"].ToString() == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }
        }
        #region 编辑添加基本操作=========================================
        private bool DoEdit(int pid)
        {
            Model.Product Pmo = new Model.Product();
            Pmo.Name = txtTitle.Value.Trim();
            Pmo.Pic = txtImgUrl_min.Value;
            if (CbIsShow.Checked)
                Pmo.DesignerID = 1;
            else
                Pmo.DesignerID = 0;
            Pmo.BigPic = txtImgUrl_max.Value;
            Pmo.ChannelID = int.Parse(ViewState["typeid"].ToString());
            Pmo.ClassID = int.Parse(ddlParentId.SelectedValue);
            Pmo.Sort = int.Parse(txt_postion.Value.Trim());
            Pmo.ID = pid;
            Pmo.jj = txtjj.Value.Trim();
            Pmo.Content = txt_Content.Value.Trim();
            Pmo.zjj = BigHeadpic.Src;
            Headpic.Src = txtImgUrl_min.Value;
            return new BLL.Product().Update(Pmo);
        }

        private bool DoAdd()
        {
            Model.Product Pmo = new Model.Product();
            Pmo.Name = txtTitle.Value.Trim();
            Pmo.Pic = txtImgUrl_min.Value;
            Pmo.BigPic = txtImgUrl_max.Value;
            if (CbIsShow.Checked)
                Pmo.DesignerID = 1;
            else
                Pmo.DesignerID = 0;

            Pmo.ChannelID = int.Parse(ViewState["typeid"].ToString());
            Pmo.ClassID = int.Parse(ddlParentId.SelectedValue);
            Pmo.Sort = int.Parse(txt_postion.Value.Trim());
            Pmo.Content = txt_Content.Value.Trim();
            Pmo.jj = txtjj.Value.Trim();
            Pmo.zjj = BigHeadpic.Src;
            Headpic.Src = txtImgUrl_min.Value;
            return new BLL.Product().Add(Pmo);
        }
        #endregion

        private void GetRequestValues()
        {
            if (ViewState["type"].ToString() == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (ViewState["type"].ToString() == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.Product().Delete(Nid);
                Response.Redirect("ProductList.aspx?typeid=" + ViewState["typeid"].ToString());
            }
            if (ViewState["type"].ToString() != KIMEnums.ActionEnum.Edit.ToString() && ViewState["type"].ToString() != KIMEnums.ActionEnum.Add.ToString() && ViewState["type"].ToString() != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
        }
        private void GetEditValue()
        {
            kim.Model.Product mo = new BLL.Product().GetModel(Nid);
            ddlParentId.SelectedValue = mo.ClassID.ToString(); ;
            txtTitle.Value = mo.Name;
            txt_Content.Value = mo.Content;
            txt_postion.Value = mo.Sort.ToString();
            Headpic.Src = mo.Pic;
            if (mo.DesignerID == 1)
                CbIsShow.Checked = true;
            else
                CbIsShow.Checked = false;
            txtImgUrl_min.Value = mo.Pic;
            txtImgUrl_max.Value = mo.BigPic;
            BigHeadpic.Src = mo.zjj;
            
            txtjj.Value= mo.jj;
        }
        protected void htn_upload_Click(object sender, EventArgs e)//上传
        {
            string strpic = PubFileAndImageFunc.fileuploadcode(this.FileUpload1, "../../pic/");
            ViewState["img"] = strpic != "" ? strpic : this.BigHeadpic.Src.ToString();
            this.BigHeadpic.Src = ViewState["img"].ToString();
        }



    }
}